class Solution {
    private int[] fa;
    public int findCircleNum(int[][] isConnected) {
        int count = isConnected.length;
        fa = new int[count];
        for (int i = 0; i < count; i++) {
            fa[i] = i;
        }
        for (int i = 0; i < count; i++) {
            for (int j = i + 1; j < count; j++) {
                if (isConnected[i][j] == 1) {
                    unionSet(i, j);
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < count; i++) {
            if (fa[i] == i) {
                ans++;
            }
        }
        return ans;
    }
    private void unionSet(int x, int y) {
        x = find(x);
        y = find(y);
        if (x != y) {
            fa[x] = y;
        }
    }
    private int find(int x) {
        if (fa[x] == x) return x;
        return fa[x] = find(fa[x]);
    }
}

/**
1 1 0
1 1 0
0 0 1
 */
